package bigbrother.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import bigbrother.entity.WorkLog;
import bigbrother.exception.ServiceFailException;

public class WorkLogDao extends Dao implements IWorkLogDao {
	Logger logger = null;
	private static WorkLogDao dao;
	private WorkLogDao() {
		setLogger();
	}
	public static WorkLogDao getDao(){
		if(dao==null){
			dao = new WorkLogDao();
		}
		return dao;
	}
	void setLogger() {
		logger = LogManager.getLogger(WorkLogDao.class);
	}

	@Override
	public int addWorkLog(Connection con, WorkLog log) {
		String query = "insert into WorkLog (id, work) values(?, ?)";
		logger.trace(query);
		PreparedStatement pstmt = null;
		int result = 0;
		try {
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, log.getEmployee());
			pstmt.setString(2, log.getWork());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close(pstmt);
		}
		return result;
	}

	@Override
	public int deleteWorkLog(Connection con, String id) {
		String query = "delete from worklog where id=?";
		logger.trace(query);
		PreparedStatement pstmt = null;
		int result = 0;
		try {
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, id);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close( pstmt);
		}
		return result;
	}

	@Override
	public WorkLog getWorkLog(Connection con, int wseq) {
		String query = "select * from worklog where workseq=?";
		logger.trace(query);
		PreparedStatement pstmt = null;
		WorkLog result = null;
		ResultSet rset = null;
		try {
			pstmt = con.prepareStatement(query);
			pstmt.setInt(1, wseq);
			rset = pstmt.executeQuery();
			if(rset.next()){
				String id = rset.getString("id");
				String work = rset.getString("work");
				Date stamp = rset.getTimestamp("stamp");
				result = new WorkLog(wseq, stamp, id, work);
			}
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close(pstmt, rset);
		}
		return result;
	}

	@Override
	public ArrayList<WorkLog> getWorkLogList(Connection con, String id) {
		String query = "select * from worklog where id=?";
		logger.trace(query);
		PreparedStatement pstmt = null;
		ArrayList<WorkLog> result = new ArrayList<WorkLog>();
		ResultSet rset = null;
		try {
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, id);
			rset = pstmt.executeQuery();
			while(rset.next()){
				int wseq = rset.getInt("workseq");
				String work = rset.getString("work");
				Date stamp = rset.getTimestamp("stamp");
				WorkLog emp= new WorkLog(wseq, stamp, id, work);
				result.add(emp);
			}
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close(pstmt, rset);
		}
		return result;
	}
}
